package edu.nyu.msis.parser;

import edu.nyu.msis.ds.Module;


public class StateMachine {
    DefListParser defListParser;
    UseListParser useListParser;
    SrcParser srcParser;
	ParserStates parserState;
	Module module;
	
	/**
	 * 
	 */
	public StateMachine() {
		super();
		parserState = ParserStates.DEF_LIST_BEGIN;
        defListParser = new DefListParser();
        useListParser = new UseListParser();
        srcParser = new SrcParser();
        module = null;
	}
	
	public void setModule(Module module) {
		this.module = module;
		defListParser.setDefList(module.getDefList());
		useListParser.setUseList(module.getUseList());
		srcParser.setModule(module);
	}

	public ParserStates parse(Character readChar) {
        switch (parserState)
        {
        	case MODULE_BEGIN:
        		{
        			parserState = defListParser.parse(readChar);
        			return ParserStates.MODULE_BEGIN;
        		}
            case DEF_LIST_BEGIN:
                {
                    parserState = defListParser.parse(readChar);
                    return ParserStates.MODULE_BEGIN;
                }
            case DEF_LIST_END:
                {
                    parserState = ParserStates.USE_LIST_BEGIN;
                    parserState = useListParser.parse(readChar);
                    return ParserStates.MODULE_BEGIN;
                }
            case USE_LIST_BEGIN:
                {
                    parserState = useListParser.parse(readChar);
                    return ParserStates.MODULE_BEGIN;
                }
            case USE_LIST_END:
                {
                    parserState = ParserStates.SRC_BEGIN;
                    parserState = srcParser.parse(readChar);
                    return ParserStates.MODULE_BEGIN;
                }
            case SRC_BEGIN:
                {
                    parserState = srcParser.parse(readChar);
                    if(parserState == ParserStates.MODULE_END){
                    	parserState = ParserStates.MODULE_BEGIN;
                    	defListParser.reset();
                    	useListParser.reset();
                    	srcParser.reset();
                    	return ParserStates.MODULE_END;
                    }
                    return ParserStates.MODULE_BEGIN;//what is this??
                }
            default:  //Should never reach here.
                {
                    System.out.println("Invalid Parser State [state: " + parserState + ", read: " + readChar + "].");
                    return ParserStates.ERROR_STATE;
                }
        }
	}
}